capture log close
log using reserve, replace


/* 
-------------------------------------------------------------------------------
					Project: Democracy and Reserves
					------------------------------
			Byunghwan Son (kalkas@gmail.com ; bit.ly/byunghwan)
						
- accepted for Publication in the Foreign Policy Analysis
- this filename: reserve_replication.do
- data filename: reserve_replication.dta
- created using Stata 14.2 
- All table and figure nmumbers correspond exactly to those that appear in the paper and online appendix
-------------------------------------------------------------------------------
*/

ssc install estout, replace
set scheme plottig



* load the data: reserve_replication.dta
xtset code year 


********************************
* Tables
********************************

* Table 2. 

xtpcse lnres l.p_polity2 l.wdi_expgsgdp  /// 
	l.lngold l.lngdp l.lngdppc l.inflation /// 
	l.d.usinter l.usinter opec ///
	l.wdi_gdpgr t t2 t_Crisis /// 
	l.noCrisis l.noCrisis2 l.noCrisis3 /// 
	, pairwise c(psar1) 	
	
	eststo base 

xtpcse lnres c.l.p_polity2##c.l.wdi_expgsgdp  /// 
	l.lngold l.lngdp l.lngdppc l.inflation /// 
	l.d.usinter l.usinter opec ///
	l.wdi_gdpgr t t2 t_Crisis /// 
	l.noCrisis l.noCrisis2 l.noCrisis3 /// 
	, pairwise c(psar1) 
	
	eststo benchmark
	
xtpcse lnres l.lnres c.l.p_polity2##c.l.wdi_expgsgdp  /// 
	l.lngold l.lngdp l.lngdppc l.inflation /// 
	l.d.usinter l.usinter opec ///
	l.wdi_gdpgr t t2 t_Crisis /// 
	l.noCrisis l.noCrisis2 l.noCrisis3 /// 
	, pairwise c(psar1) 
	
	eststo ldv
	
xtregar lnres c.l.p_polity2##c.l.wdi_expgsgdp  /// 
	l.lngold l.lngdp l.lngdppc l.inflation /// 
	l.d.usinter l.usinter opec ///
	l.wdi_gdpgr t t2 t_Crisis /// 
	l.noCrisis l.noCrisis2 l.noCrisis3  /// 
	, fe 
	
	eststo fe2		
	
********************************

* Table 3.

xtpcse res_gdp c.l.p_polity2##c.l.wdi_expgsgdp  /// 
	l.lngold l.inflation /// 
	l.d.usinter l.usinter opec ///
	l.wdi_gdpgr t t2 t_Crisis /// 
	l.noCrisis l.noCrisis2 l.noCrisis3 /// 
	, pairwise c(psar1) 
	
	eststo gdp	
	
xtpcse reserve_money c.l.p_polity2##c.l.wdi_expgsgdp  /// 
	l.lngold l.lngdp l.lngdppc l.inflation /// 
	l.d.usinter l.usinter opec ///
	l.wdi_gdpgr t t2 t_Crisis /// 
	l.noCrisis l.noCrisis2 l.noCrisis3 /// 
	, pairwise c(psar1) 	
	
	eststo money
	
xtpcse total_res_imp c.l.p_polity2##c.l.wdi_expgsgdp  /// 
	l.lngold l.lngdp l.lngdppc l.inflation /// 
	l.d.usinter l.usinter opec ///
	l.wdi_gdpgr t t2 t_Crisis /// 
	l.noCrisis l.noCrisis2 l.noCrisis3 /// 
	, pairwise c(psar1) 
	
	eststo import

********************************

* Table 4.

xtpcse lnres c.l.p_polity2##c.l.wdi_expman  /// 
	l.lngold l.lngdp l.lngdppc l.inflation /// 
	l.d.usinter l.usinter opec ///
	l.wdi_gdpgr t t2 t_Crisis /// 
	l.noCrisis l.noCrisis2 l.noCrisis3 /// 
	, pairwise c(psar1)

	eststo manu1
	
xtpcse lnres c.l.p_polity2##c.l.manu  /// 
	l.lngold l.lngdp l.lngdppc l.inflation /// 
	l.d.usinter l.usinter opec ///
	l.wdi_gdpgr t t2 t_Crisis /// 
	l.noCrisis l.noCrisis2 l.noCrisis3 /// 
	, pairwise c(psar1)
	
	eststo manu2
	
xtpcse lnres c.l.p_polity2##c.l.nohi  /// 
	l.lngold l.lngdp l.lngdppc l.inflation /// 
	l.d.usinter l.usinter opec ///
	l.wdi_gdpgr t t2 t_Crisis /// 
	l.noCrisis l.noCrisis2 l.noCrisis3 /// 
	, pairwise c(psar1)
	
	eststo tech	
	
xtpcse lnres c.l.p_polity2##c.l.lnexport  /// 
	l.lngold l.lngdp l.lngdppc l.inflation /// 
	l.d.usinter l.usinter opec ///
	l.wdi_gdpgr t t2 t_Crisis /// 
	l.noCrisis l.noCrisis2 l.noCrisis3 /// 
	, pairwise c(psar1)
	
	eststo lnexport
	
xtpcse lnres c.l.p_polity2##c.exgro2  /// 
	l.lngold l.lngdp l.lngdppc l.inflation /// 
	l.d.usinter l.usinter opec ///
	l.wdi_gdpgr t t2 t_Crisis /// 
	l.noCrisis l.noCrisis2 l.noCrisis3 /// 
	, pairwise c(psar1)
	
	eststo exgro2

********************************

* Table 5.

** Monarchy is the baseline
xtpcse lnres i.l.ht_regtype1##c.l.wdi_expgsgdp  /// 
	l.lngold l.lngdp l.lngdppc l.inflation /// 
	l.d.usinter l.usinter opec ///
	l.wdi_gdpgr t t2 t_Crisis /// 
	l.noCrisis l.noCrisis2 l.noCrisis3 /// 
	, pairwise c(psar1)
	
	eststo hadeinus
	
xtpcse lnres c.l.vdem_polyarchy##c.l.wdi_expgsgdp  /// 
	l.lngold l.lngdp l.lngdppc l.inflation /// 
	l.d.usinter l.usinter opec ///
	l.wdi_gdpgr t t2 t_Crisis /// 
	l.noCrisis l.noCrisis2 l.noCrisis3 /// 
	, pairwise c(psar1)	

	eststo vdem
	
xtpcse lnres c.l.dpi_eipc##c.l.wdi_expgsgdp  /// 
	l.lngold l.lngdp l.lngdppc l.inflation /// 
	l.d.usinter l.usinter opec ///
	l.wdi_gdpgr t t2 t_Crisis /// 
	l.noCrisis l.noCrisis2 l.noCrisis3 /// 
	, pairwise c(psar1)
	
	eststo dpi

xtpcse lnres i.l.chga_demo##c.l.wdi_expgsgdp  ///
	l.lngold l.lngdp l.lngdppc l.inflation /// 
	l.d.usinter l.usinter opec ///
	l.wdi_gdpgr t t2 t_Crisis /// 
	l.noCrisis l.noCrisis2 l.noCrisis3 /// 
	, pairwise c(psar1)
		
	eststo dd	
	
xtpcse lnres c.l.fh2##c.l.wdi_expgsgdp  ///
	l.lngold l.lngdp l.lngdppc l.inflation /// 
	l.d.usinter l.usinter opec ///
	l.wdi_gdpgr t t2 t_Crisis /// 
	l.noCrisis l.noCrisis2 l.noCrisis3 /// 
	, pairwise c(psar1)	
	
	eststo fh	

********************************

* Table 6.


local var "Garriga Bodea_Hicks"
foreach X of local var {
xtpcse lnres c.l.p_polity2##c.l.wdi_expgsgdp  /// 
	l.lngold l.lngdp l.lngdppc l.inflation /// 
	l.d.usinter l.usinter opec ///
	l.wdi_gdpgr t t2 t_Crisis /// 
	l.noCrisis l.noCrisis2 l.noCrisis3 `X' /// 
	, pairwise c(psar1) 	
	
	eststo `X'
}
*	

********************************
* Regression Tables
* Table 2
esttab base benchmark ldv fe2, /// 
title(Benchmark) long replace se(3) /// 
star(* 0.10 ** 0.05 *** 0.01) r2 brackets b(3) label

* Table 3 (abbreviated in the manuscript)
esttab gdp money import , /// 
title("Scaled Reserve Variables") long replace se(3) /// 
star(* 0.10 ** 0.05 *** 0.01) r2 brackets b(3) label
********************************************** ----> also Table A4 (full table).

* Table 4
esttab manu1 manu2 tech lnexport  exgro2 , /// 
title("Alternative Conditioning Variables") long replace se(3) /// 
star(* 0.10 ** 0.05 *** 0.01) r2 brackets b(3) label
*********************************************** ----> also Table A8 (full table)

* Table 5 (abbreviated in the manuscript) 
esttab hadeinus vdem dpi dd fh , /// 
title("Alternative Regime Variables") long replace se(3) /// 
star(* 0.10 ** 0.05 *** 0.01) r2 brackets b(3) label
*********************************************** ----> also Table A5 (full table)


* Table 6 (abbreviated in the manuscript)	
esttab Garriga Bodea_Hicks using , /// 
title(CBI) wide replace se(3) /// 
star(* 0.10 ** 0.05 *** 0.01) r2 brackets b(3) label
*********************************************** ----> also Table A6 (full table)

********************************

************************************************************************************************
* Figures
************************************************************************************************
* Figure 1.

gen res100 = res_xg/1000000000000 
label variable res100 "Forex Reserve, $ tn"
sort year
by year: egen y_res = mean(lnres) 
by year: egen y_res2 = mean(res100) if country !="China"
by year: egen sum = sum(res100) 
label variable sum "Reserve, Global, $ tn"
label variable y_res "Reserve, Global Mean, $ tn"
by year: egen y_pol = mean(p_polity2) 
label variable y_pol "Polity2, Global"
xtset code year 

cd "$data"
twoway (line sum year, lwidth(thick) lcolor(blue) yaxis(1) sort(year)) /// 
	(line y_pol year, yaxis(2) sort(year) lwidth(thick) lpattern(dash) lcolor(red)) /// 
	(scatter res100 year, yaxis(1) msize(verysmall) msymbol(oh) mcolor(black) ) /// 
	(scatter res100 year if res100>0.5 & year==2013, yaxis(1) msymbol(none) mcolor(black) mlabel(country)) /// 
	if year>1974 & year<2014, /// 
	xlabel(1975 (10) 2015) saving(trend2, replace)  ///
	ytitle("Forex Reserve, $ tn", ax(1)) ///
	legend(order(1 "Reserves" 4 "Polity") ring(0) position(11))
		gr export trend2.pdf, replace

********************************

* Figure 3.

xtpcse lnres c.l.p_polity2##c.l.wdi_expgsgdp  /// 
	l.lngold l.lngdp l.lngdppc l.inflation /// 
	l.d.usinter l.usinter opec ///
	l.wdi_gdpgr t t2 t_Crisis /// 
	l.noCrisis l.noCrisis2 l.noCrisis3 /// 
	, pairwise c(psar1) 

margins, dydx(l.p_polity2) at(l.wdi_expgsgdp=(6 (10) 170) )
marginsplot,  recast(line) recastci(rarea) scheme(plotplainblind) /// 
	plotopts(lpattern(solid) lc(blue) lw(thick)) ///	
	ci1opts(lc(white) fintensity(25)) yline(0) /// 
	saving(benchmark, replace) ytitle("ln(reserve)") ///
	title("") xtitle("export/GDP(%)") xlabel(5 (25) 165) ///
	addplot(hist wdi_expgsgdp if e(sample) & wdi_expgsgdp<165,  /// 
		ylabel(none, ax(2)) ytitle(" ",ax(2))  xlabel(5 (25) 165) ///
		yaxis(2) yscale(alt r(0 0.2) ax(2)) fcolor(white)) title("") legend(off)
gr export benchmark.png, replace	
	
********************************

* Figure 4

xtpcse lnres c.l.p_polity2##c.l.wdi_expgsgdp  /// 
	l.lngold l.lngdp l.lngdppc l.inflation /// 
	l.d.usinter l.usinter opec ///
	l.wdi_gdpgr t t2 t_Crisis /// 
	l.noCrisis l.noCrisis2 l.noCrisis3 /// 
	, pairwise c(psar1) 

margins, at(l.wdi_expgsgdp=(6 (10) 170) l.p_polity2=(-2 10) )
	
marginsplot, ytitle("ln(reserve)")  recast(line) recastci(rarea) /// 
	plot1opts(lpattern(solid) lc(red) lw(thick)) ///	
	ci1opts(lc(white) fintensity(25)) ///
	plot2opts(lpattern(dash) lc(blue) lw(thick)) ///	
	ci2opts(lc(white) fintensity(40)) ///
	saving(temp1, replace) ///
	title("") legend(order(3 "polity=-2" 4 "polity=10") ring(0) position(5)) /// 
			xtitle("export/GDP(%)") xlabel(0 25 50 75 100 125 150 175) 
	
	gr export hypo.png, replace

********************************



************************************************************************************************
* Appendix Tables and Figures

********************************

* Table A1.

reg lnres c.l.p_polity2##c.l.wdi_expgsgdp  /// 
	l.lngold l.lngdp l.lngdppc l.inflation /// 
	l.d.usinter l.usinter opec ///
	l.wdi_gdpgr t t2 t_Crisis /// 
	l.noCrisis l.noCrisis2 l.noCrisis3
	
	sum lnres c.l.p_polity2 l.wdi_expgsgdp  /// 
	l.lngold l.lngdp l.lngdppc l.inflation /// 
	l.d.usinter l.usinter opec ///
	l.wdi_gdpgr t t2 t_Crisis /// 
	l.noCrisis l.noCrisis2 l.noCrisis3 /// 
	if e(sample) & p_polity2>5
	
	sum lnres c.l.p_polity2 l.wdi_expgsgdp  /// 
	l.lngold l.lngdp l.lngdppc l.inflation /// 
	l.d.usinter l.usinter opec ///
	l.wdi_gdpgr t t2 t_Crisis /// 
	l.noCrisis l.noCrisis2 l.noCrisis3 /// 
	if e(sample) & p_polity2<6
	
********************************

* Table A2. 

xtpcse lnres c.l.p_polity2##c.l.wdi_expgsgdp  ///
	l.lngold l.lngdp l.lngdppc l.inflation /// 
	l.d.usinter l.usinter opec ///
	l.wdi_gdpgr t t2 t_Crisis /// 
	l.noCrisis l.noCrisis2 l.noCrisis3 RR1-RR5	///
	, pairwise c(psar1)
	
	eststo err
	
local var "h_polcon5 icrg_qog hrv_index election financial imf_ab"
foreach X of local var {

xtpcse lnres c.l.p_polity2##c.l.wdi_expgsgdp  ///
	l.lngold l.lngdp l.lngdppc l.inflation /// 
	l.d.usinter l.usinter opec ///
	l.wdi_gdpgr t t2 t_Crisis /// 
	l.noCrisis l.noCrisis2 l.noCrisis3 `X' /// 
	, pairwise c(psar1)
	
 eststo `X', nocopy
}
*

********************************

* Table A3.

xtpcse lnres c.l.p_polity2##c.l.wdi_expgsgdp  ///
	l.lngold l.lngdp l.lngdppc l.inflation /// 
	l.d.usinter l.usinter opec ///
	l.wdi_gdpgr t t2 t_Crisis /// 
	l.noCrisis l.noCrisis2 l.noCrisis3 /// 
	if euro==0, pairwise c(psar1)	

	eststo noeuro
	
xtpcse lnres c.l.p_polity2##c.l.wdi_expgsgdp  ///
	l.lngold l.lngdp l.lngdppc l.inflation /// 
	l.d.usinter l.usinter opec ///
	l.wdi_gdpgr t t2 t_Crisis /// 
	l.noCrisis l.noCrisis2 l.noCrisis3 /// 
	if regioncode !="NAC" & regioncode !="ECS" ///
	, pairwise c(psar1)	

	eststo nowest	
	
xtpcse lnres c.l.p_polity2##c.l.wdi_expgsgdp  ///
	/* l.lngold */ l.lngdp l.lngdppc /* l.inflation */ /// 
	l.d.usinter l.usinter opec ///
	l.wdi_gdpgr t t2 t_Crisis /// 
	l.noCrisis l.noCrisis2 l.noCrisis3 /// 
	, pairwise c(psar1)	
	
	eststo saturated

********************************

* Figure A1.

xtpcse lnres c.l.p_polity2##c.l.t_Crisis l.wdi_expgsgdp /// 
	l.lngold l.lngdp l.lngdppc l.inflation l.d.usinter l.usinter ///
	l.wdi_gdpgr t t2   l.noCrisis l.noCrisis2 l.noCrisis3 opec, /// 
	pairwise c(psar1) 
 
 eststo ins1
 
margins, dydx(l.p_polity2) at(l.t_Crisis=(10(5)70)) level(95)
marginsplot, yline(0) recast(line) recastci(rline) /// 
			ci1opts(lpattern(dash) fintensity(10)) ///
			name(ins1, replace) title("") /// 
			ytitle("ln(reserves)") xlabel(10 30 50 70) ///
			xtitle("Number of Past Crises") /// 
			addplot(hist t_Crisis if e(sample)  & t_Crisis<71 ,  /// 
			ylabel(none, ax(2)) ytitle(" ",ax(2))  xlabel(10 30 50 70) ///
			yaxis(2) yscale(alt r(0 0.5) ax(2)) fcolor(white)) legend(off)
			
			gr export ins.pdf, replace

********************************

* Table A7.

xtpcse lnres c.l.p_polity2##c.l.wdi_expgsgdp  /// 
	l.lngold l.lngdp l.lngdppc l.inflation /// 
	l.d.usinter l.usinter opec ///
	l.wdi_gdpgr /* t t2 */ t_Crisis /// 
	l.noCrisis l.noCrisis2 l.noCrisis3 c.year##i.code /// 
	, pairwise c(psar1) 
	
	eststo fetrend
	
xtpcse lnres c.l.p_polity2##c.l.wdi_expgsgdp  /// 
	l.lngold l.lngdp l.lngdppc l.inflation /// 
	l.d.usinter l.usinter opec ///
	l.wdi_gdpgr /* t t2 */ t_Crisis /// 
	l.noCrisis l.noCrisis2 l.noCrisis3 ///
	i.year , pairwise c(psar1) 
	
	eststo yearfe
	
xtpcse lnres c.l.p_polity2##c.l.wdi_expgsgdp  /// 
	l.lngold l.lngdp l.lngdppc l.inflation /// 
	l.d.usinter l.usinter opec ///
	l.wdi_gdpgr /* t t2 */ t_Crisis /// 
	l.noCrisis l.noCrisis2 l.noCrisis3 ///
	i.code , pairwise c(psar1) 
	
	eststo countryfe	
	

xtreg lnres l.lnres c.l.p_polity2##c.l.wdi_expgsgdp  /// 
	l.lngold l.lngdp l.lngdppc l.inflation /// 
	l.d.usinter l.usinter opec ///
	l.wdi_gdpgr  t t2 t_Crisis /// 
	l.noCrisis l.noCrisis2 l.noCrisis3, re	
	
	eststo re
 
********************************

* Figure A2.


local var2 "DPI FreedomHouse VDem"
foreach Z of local var2 {
xtpcse lnres c.l.`Z'##c.l.wdi_expgsgdp  /// 
	l.lngold l.lngdp l.lngdppc l.inflation /// 
	l.d.usinter l.usinter opec ///
	l.wdi_gdpgr t t2 t_Crisis /// 
	l.noCrisis l.noCrisis2 l.noCrisis3 /// 
	, pairwise c(psar1)	
	
	margins, dydx(l.`Z') at(l.wdi_expgsgdp=(6 (10) 170))
	
	marginsplot,  recast(line) recastci(rarea) scheme(plotplainblind) /// 
	plotopts(lpattern(solid) lc(blue) lw(thick)) ///	
	ci1opts(lc(white) fintensity(25)) yline(0) /// 
	name(`Z', replace) ytitle("ln(reserve)") xtitle("export/GDP(%)") ///
	xlabel(10 30 50 70 90 110 130) title(`Z')
	
	gr export `Z'.png, replace		
} 
*

xtpcse lnres i.l.chga_demo##c.l.wdi_expgsgdp  /// 
	l.lngold l.lngdp l.lngdppc l.inflation /// 
	l.d.usinter l.usinter opec ///
	l.wdi_gdpgr t t2 t_Crisis /// 
	l.noCrisis l.noCrisis2 l.noCrisis3 /// 
	, pairwise c(psar1)	
	
	margins, dydx(l.chga_demo) at(l.wdi_expgsgdp=(6 (10) 170))
	
	marginsplot,  recast(line) recastci(rarea) scheme(plotplainblind) /// 
	plotopts(lpattern(solid) lc(blue) lw(thick)) ///	
	ci1opts(lc(white) fintensity(25)) yline(0) /// 
	name(dd, replace) ytitle("ln(reserve)") xtitle("export/GDP(%)") ///
	xlabel(10 30 50 70 90 110 130) title("DD index")
	
	
	gr export dd.png, replace	

	
gr combine 	DPI FreedomHouse VDem dd, c(2) 
gr export combined_demo.png, replace

********************************

* table A2
esttab err h_polcon5 icrg_qog hrv_index financial election imf_ab, /// 
title("Additional Controls") long replace se(3) /// 
star(* 0.10 ** 0.05 *** 0.01) r2 brackets b(3) label


* table A3
esttab noeuro nowest saturated , /// 
title("Different Samples") long replace se(3) /// 
star(* 0.10 ** 0.05 *** 0.01) r2 brackets b(3) label


* table A7
esttab fetrend yearfe countryfe re  , /// 
title("Fixed and Random Effects") long replace se(3) /// 
star(* 0.10 ** 0.05 *** 0.01) r2 brackets b(3) label

********************************
log close
